#!/usr/bin/perl

use warnings;
use strict;

my %complem = ( "a" => "t",
		"t" => "a",
		"c" => "g",
		"g" => "c",
		" " => " "
 		);
		
		
my $file_name=$ARGV[0];
my $file_name_comple=$file_name."_comp";

open(HANDLE,$file_name)|| die "can't opendir $file_name: $!";
open(HANDLECOMPL,">$file_name_comple")|| die "can't opendir $file_name_comple: $!";

seek(HANDLE,0,2);

my $current_position=tell(HANDLE);
$current_position-=1;
seek(HANDLE,$current_position,0);

my $charCount=0;
my $lineNucleotidos="";
while ( ($current_position >= 0) && (read(HANDLE,my $char,1)))
{
 $current_position-=1;
 seek(HANDLE,$current_position,0);
 
 if ($char =~ m/\b(a|t|c|g)/)
  { 
	$charCount+=1;
	if($charCount == 60)
	{
	print HANDLECOMPL "$lineNucleotidos\n";
	$charCount=1;
	$lineNucleotidos="";
	}
 	else 
	{
	   $lineNucleotidos=join('',$lineNucleotidos,$complem{$char});
	}
  }
  
}
print HANDLECOMPL  "$lineNucleotidos\n";


